# *_*coding:utf-8 *_*
# @Author : Reggie
# @Time : 2022/9/3 17:07
import json
import time
from pathlib import Path

from dataclasses import dataclass

from base_m3u8 import DownLoad_M3U8


def get_m3u8_url(string):
    return json.loads(string).get("video").get("url")


def get_m3u8_url_by_file(file_name):
    with open(file_name, encoding="utf-8") as f:
        return get_m3u8_url(f.read())


@dataclass
class Zztt24(DownLoad_M3U8):
    def download_all_ts(self):
        self.download_all_ts2()


def main():
    # 使用系统变量修改代理, 可以让m3u8解析也经过代理
    # os.environ["http_proxy"] = "http://127.0.0.1:10809"
    # os.environ["https_proxy"] = "https://127.0.0.1:10809"
    # print(os.environ)
    m3u8_url = 'https://m3u8.zztt85.com/watch9/5d61b6aefd47c15b95dbfd7608b527c0/5d61b6aefd47c15b95dbfd7608b527c0.m3u8'
    # m3u8_url = get_m3u8_url(
    #     '''{"live":false,"autoplay":false,"theme":"#FADFA3","loop":false,"screenshot":false,"hotkey":true,"preload":"metadata","lang":"zh-cn","logo":null,"volume":0.7,"mutex":true,"video":{"url":"https:\/\/0x3.maoxiaomao.top\/media\/upload-video\/855\/855e038f6d42716d78c947274e9222f8\/m3u8\/index.m3u8?_v=20211203&sign=1662705316-44cc1fc20dd37532a13ed8f8eadf0c77-0-ba89929c07bec23cc9806144ef192b9e","pic":"","type":"auto","thumbnails":null}}''')
    m3u8_url = get_m3u8_url_by_file("zztt24.json")
    file_name = '美籍华人艺术家kelly王佳怡18岁视频暗黑往事三部曲（二）'
    out_path = Path(r"C:\download").joinpath(Path(__file__).stem)
    proxies = {
        'sock5': '127.0.0.1:10808',
        'http': '127.0.0.1:10809',
        'https': '127.0.0.1:10809'
    }
    # proxies = {}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36'
    }
    download_proxies = proxies
    download_headers = {
        "Referer": "https://zztt24.com/",
        "origin": "https://zztt24.com",
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 dingtalk-win/1.0.0 nw(0.14.7) DingTalk(4.6.39-Release.10) Mojo/1.0.0 Native AppType(release)',
    }

    start = time.time()
    M3U8 = Zztt24(
        m3u8_url=m3u8_url,
        file_name=file_name,
        out_path=out_path,
        ts_path=ts_path,
        proxies=proxies,
        rm_ts=False,
        headers=headers,
        max_workers=1,
        download_headers=download_headers,
        download_proxies=download_proxies,
        check_all_ts=True,
        merge_ts=True,
    )
    # M3U8.merge_all_ts(need_decode=True)
    # print(M3U8.config)
    # print(len(M3U8.config.keys()))
    M3U8.run()
    end = time.time()
    print('耗时:', end - start)


if __name__ == '__main__':
    main()
